package com.ruoyi.web.controller.glue;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GlueIngredients;
import com.ruoyi.system.service.IGlueIngredientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 辅料信息
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/system/ingredients")
public class GlueIngredientsController extends BaseController
{
    @Autowired
    private IGlueIngredientsService glueIngredientsService;

    /**
     * 查询辅料信息列表
     * @return {@link GlueIngredients}
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:list')")
    @GetMapping("/list")
    public TableDataInfo list(GlueIngredients glueIngredients)
    {
        startPage();
        List<GlueIngredients> list = glueIngredientsService.selectGlueIngredientsList(glueIngredients);
        return getDataTable(list);
    }


    /**
     * 导入辅料信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:import')")
    @Log(title = "辅料信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importGlueIngredients(@RequestBody MultipartFile file)
    {
        ExcelUtil<GlueIngredients> util = new ExcelUtil<GlueIngredients>(GlueIngredients.class);
        try {
            Date nowDate = DateUtils.getNowDate();
            List<GlueIngredients> glueIngredients = util.importExcel(file.getInputStream());
            if (!CollectionUtils.isEmpty(glueIngredients)){
                glueIngredients=glueIngredients.stream().filter(item->!ObjectUtils.isEmpty(item)).collect(Collectors.toList());
            }
                glueIngredients.forEach(item->{
                      item.setCreateBy(getUserId().toString());
                      item.setCreateTime(nowDate);
                });
            if (CollectionUtils.isEmpty(glueIngredients)){
                throw new RuntimeException("没有读取到表格数据，请检查表格表头是否与提供的模板表头一致。");
            }
                glueIngredientsService.saveBatch(glueIngredients);

        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.success(e.getMessage());
        }
        return AjaxResult.success();
    }




    /**
     * 导出辅料信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:export')")
    @Log(title = "辅料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GlueIngredients glueIngredients)
    {
        List<GlueIngredients> list = glueIngredientsService.selectGlueIngredientsList(glueIngredients);
        ExcelUtil<GlueIngredients> util = new ExcelUtil<GlueIngredients>(GlueIngredients.class);
        util.exportExcel(response, list, "辅料信息数据");
    }


    /**
     * 下载辅料导入模板
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:import')")
    @Log(title = "辅料信息", businessType = BusinessType.IMPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response)
    {
        ExcelUtil<GlueIngredients> util = new ExcelUtil<GlueIngredients>(GlueIngredients.class);
        util.exportExcel(response, null, "基材导入模板");
    }




    /**
     * 获取辅料信息详细信息
     * @return {@link GlueIngredients}
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(glueIngredientsService.selectGlueIngredientsById(id));
    }

    /**
     * 新增辅料信息
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:add')")
    @Log(title = "辅料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GlueIngredients glueIngredients)
    {
        return toAjax(glueIngredientsService.insertGlueIngredients(glueIngredients));
    }

    /**
     * 修改辅料信息
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:edit')")
    @Log(title = "辅料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GlueIngredients glueIngredients)
    {
        return toAjax(glueIngredientsService.updateGlueIngredients(glueIngredients));
    }

    /**
     * 删除辅料信息
     */
    //@PreAuthorize("@ss.hasPermi('system:ingredients:remove')")
    @Log(title = "辅料信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(glueIngredientsService.deleteGlueIngredientsByIds(ids));
    }


    /**
     * 辅料名称三级目录
     * @param dto
     * @return
     */
    @GetMapping("/getModel")
    public AjaxResult getModel(String dto){
        List<String> list = glueIngredientsService.getModel(dto);
        return AjaxResult.success(list);
    }



}
